#!/bin/bash


shellout(){
 echo 
 echo "ERROR $? in $1 commande";
 echo
 exit 2;
}

LSCMD=`whereis ls | awk '{print $1}'`
ARCH=`file "$LSCMD"`

if echo "$ARCH" | grep -q "x86_64"
then
	ARCH="x86_64"
else
	ARCH="i386"
fi

#export VERSION=0.5.3
#export VERSION=checkout-2011-02-05
export VERSION=0.6.1
export SRC=/usr/local/src/kakola-tools
mkdir -p $SRC
cd $SRC

export TOOLS_MULTIMEDIA_DIR=/usr/local/kakola-tools-$VERSION-$ARCH
#mkdir -p "$TOOLS_MULTIMEDIA_DIR"
#cp /Volumes/System-32/usr/local/src/ffmpeg/*.gz $SRC/
#cp /Volumes/System-32/usr/local/src/ffmpeg/*.bz2 $SRC/

export PATH=${TOOLS_MULTIMEDIA_DIR}/bin:${PATH}
export LD_LIBRARY_PATH=${TOOLS_MULTIMEDIA_DIR}/lib:${LD_LIBRARY_PATH}
export PKG_CONFIG_PATH=${TOOLS_MULTIMEDIA_DIR}/lib/pkgconfig/:${PKG_CONFIG_PATH}

#echo OPEN-AMR
#cd $SRC
#curl -O -C - "http://sourceforge.net/projects/opencore-amr/files/opencore-amr/0.1.2/opencore-amr-0.1.2.tar.gz/download"
#tar xvzf opencore-amr-0.1.2.tar.gz 
#cd opencore-amr-0.1.2
#./configure --prefix=$TOOLS_MULTIMEDIA_DIR --enable-shared=no --enable-static=yes && make && make install

GITTARGET=/usr/local/git-${ARCH}
cd ${SRC}
curl -O -C - http://kakola.googlecode.com/files/git-1.7.3.5.tar.bz2
[ -d "git-1.7.3.5" ] || tar xvjf git-1.7.3.5.tar.bz2
cd git-1.7.3.5
./configure --prefix=${GITTARGET} || shellout "./configure --prefix=${GITTARGET}"
make || shellout "make git"
make install || shellout "make install git"
PATH=${GITTARGET}/bin:$PATH

#YASMTARGET=/usr/local/yasm-${ARCH}
#echo YASM
#cd $SRC
#curl -O -C - http://www.tortall.net/projects/yasm/releases/yasm-1.0.0.tar.gz
#tar xvzf yasm-1.0.0.tar.gz 
#cd yasm-1.0.0
#./configure --prefix=$YASMTARGET --enable-shared=no --enable-static=yes || shellout "./configure --prefix=$YASMTARGET --enable-shared=no --enable-static=yes"
#make || shellout "make yasm-1.0.0"
#make install || shellout "make install yasm-1.0.0"
#PATH=${YASMTARGET}/bin:$PATH

#NASMTARGET=/usr/local/nasm-${ARCH}
#echo NASM
#cd $SRC
##curl -O -C - http://www.nasm.us/pub/nasm/releasebuilds/2.08.01/nasm-2.08.01.tar.bz2
#tar xvjf nasm-2.08.01.tar.bz2
#cd nasm-2.08.01
#./configure --prefix=$NASMTARGET --enable-shared=no --enable-static=yes  || shellout "./configure --prefix=$NASMTARGET --enable-shared=no --enable-static=yes"
#make || shellout "make nasm-2.08.01"
#make install || shellout "make install  nasm-2.08.01"
#PATH=${NASMTARGET}/bin:$PATH


echo FAAC
cd $SRC
curl -O -C - "http://kakola.googlecode.com/files/faac-1.28.tar.gz"
[ -d "faac-1.28" ] || tar xvzf faac-1.28.tar.gz
cd faac-1.28
./configure --prefix=$TOOLS_MULTIMEDIA_DIR --enable-shared=no --enable-static=yes --with-mp4v2  || shellout "./configure faac-1.28"
make || shellout "make faac-1.28"
make install || shellout "make install faac-1.28"

echo FAAD
cd $SRC
curl -O -C - "http://kakola.googlecode.com/files/faad2-2.7.tar.gz"
[ -d "faad2-2.7" ] || tar xvzf faad2-2.7.tar.gz 
cd faad2-2.7
./configure --prefix=$TOOLS_MULTIMEDIA_DIR --enable-shared=no --enable-static=yes  --with-mpeg4ip --with-drm  || shellout "./configure faad2-2.7"
make  || shellout "maek faad2-2.7"
make install || shellout "make install faad2-2.7"

echo "OGG, VORBIS et THEORA"
cd $SRC
curl -O -C - http://kakola.googlecode.com/files/libao-1.0.0.tar.gz
[ -d "libao-1.0.0" ] || tar xvzf libao-1.0.0.tar.gz 
cd libao-1.0.0
./configure --prefix=$TOOLS_MULTIMEDIA_DIR --enable-shared=no --enable-static=yes   || shellout ""
make  
make install 

cd $SRC
curl -O -C - http://kakola.googlecode.com/files/libogg-1.2.2.tar.gz
[ -d "libogg-1.2.2" ] || tar xvzf libogg-1.2.2.tar.gz 
cd libogg-1.2.2
./configure --prefix=$TOOLS_MULTIMEDIA_DIR --enable-shared=no --enable-static=yes   || shellout "./configure libogg-1.2.2"
make  || shellout "maek libogg-1.2.2"
make install || shellout "make install libogg-1.2.2"

cd $SRC
curl -O -C - http://kakola.googlecode.com/files/libvorbis-1.3.2.tar.bz2
[ -d "libvorbis-1.3.2" ] || tar xvjf libvorbis-1.3.2.tar.bz2 
cd libvorbis-1.3.2
./configure --prefix=$TOOLS_MULTIMEDIA_DIR --enable-shared=no --enable-static=yes   || shellout "./configure libvorbis-1.3.2"
make  || shellout "make libvorbis-1.3.2"
make install || shellout "make install libvorbis-1.3.2"

cd $SRC
curl -O -C - http://kakola.googlecode.com/files/libtheora-1.1.1.tar.bz2
[ -d "libtheora-1.1.1" ] || tar xvjf libtheora-1.1.1.tar.bz2  
cd libtheora-1.1.1
./configure --prefix=$TOOLS_MULTIMEDIA_DIR --enable-shared=no --enable-static=yes --with-ogg=${TOOLS_MULTIMEDIA_DIR} --with-vorbis=${TOOLS_MULTIMEDIA_DIR}/    || shellout "./configure libtheora-1.1.1"
make  || shellout "make libtheora-1.1.1"
make install || shellout "make install libtheora-1.1.1"



#echo SPEEX
#C'esr cette version qu'il faut et non la 1.0.5 car elle ne se compilera pas
#cd $SRC
#curl -O -C - http://downloads.xiph.org/releases/speex/speex-1.2rc1.tar.gz
#tar xvzf speex-1.2rc1.tar.gz 
#cd speex-1.2rc1
# ./configure --prefix=$TOOLS_MULTIMEDIA_DIR --enable-shared=no --enable-static=yes --with-ogg=${TOOLS_MULTIMEDIA_DIR} --with-vorbis=${TOOLS_MULTIMEDIA_DIR}
#make && make install


echo LAME
cd $SRC
curl -O -C - http://kakola.googlecode.com/files/lame-3.98.4.tar.gz
[ -d "lame-3.98.4" ] || tar xvzf lame-3.98.4.tar.gz 
cd lame-3.98.4
./configure --prefix=$TOOLS_MULTIMEDIA_DIR --enable-shared=no --enable-static=yes --enable-nasm  || shellout "./configure lame-3.98.4"
make  || shellout "make lame-3.98.4"
make install || shellout "make install lame-3.98.4"

echo A52
cd $SRC
curl -O -C - http://kakola.googlecode.com/files/a52dec-0.7.4.tar.gz
[ -d "a52dec-0.7.4" ] || tar xvzf a52dec-0.7.4.tar.gz 
cd a52dec-0.7.4
./configure --prefix=$TOOLS_MULTIMEDIA_DIR --enable-shared=no --enable-static=yes   || shellout "./configure a52dec-0.7.4"
make  || shellout "make a52dec-0.7.4"
make install || shellout "make install a52dec-0.7.4"

echo XVID
cd $SRC
curl -O -C - http://kakola.googlecode.com/files/xvidcore-1.2.2.tar.gz
[ -d "xvidcore" ] || tar xvzf xvidcore-1.2.2.tar.gz 
cd xvidcore
cd build/generic/
./configure --prefix=$TOOLS_MULTIMEDIA_DIR --enable-shared=no --enable-static=yes   || shellout "./configure xvidcore-1.2.2"
make  || shellout "make xvidcore-1.2.2"
make install || shellout "make install xvidcore-1.2.2"

echo X264
cd $SRC
#curl -O -C - http://downloads.videolan.org/pub/videolan/x264/snapshots/x264-snapshot-20100420-2245.tar.bz2
curl -O -C - http://kakola.googlecode.com/files/x264-snapshot-20110204-2245.tar.bz2
#tar xvjf x264-snapshot-20100420-2245.tar.bz2
[ -d "x264-snapshot-20110204-2245" ] || tar xvjf x264-snapshot-20110204-2245.tar.bz2
cd x264-snapshot-20110204-2245
#./configure --prefix=$TOOLS_MULTIMEDIA_DIR --enable-shared=no --enable-static=yes  && make && make install
./configure --prefix=$TOOLS_MULTIMEDIA_DIR --disable-asm  || shellout "./configure x264-snapshot-20110204-2245"
make  || shellout "make x264-snapshot-20110204-2245"
make install || shellout "make install x264-snapshot-20110204-2245"

echo LIBVPX
cd $SRC
curl -O -C - http://kakola.googlecode.com/files/libvpx-v0.9.5.tar.bz2
[ -d "libvpx-v0.9.5" ] || tar xvjf libvpx-v0.9.5.tar.bz2
cd libvpx-v0.9.5
./configure --prefix=$TOOLS_MULTIMEDIA_DIR --enable-vp8  
make  
make install 
#DIRAC et SCHROEDINGER
#cd $SRC
#curl -O -C - http://sourceforge.net/projects/dirac/files/dirac-codec/Dirac-1.0.2/dirac-1.0.2.tar.gz/download
#tar xvzf dirac-1.0.2.tar.gz 
#cd dirac-1.0.2
#./configure --prefix=$TOOLS_MULTIMEDIA_DIR --enable-shared=no --enable-static=yes && make && make install

#cd $SRC
#curl -O -C - http://code.entropywave.com/download/orc/orc-0.4.4.tar.gz
#tar xvzf orc-0.4.4.tar.gz 
#cd orc-0.4.4
#./configure --prefix=$TOOLS_MULTIMEDIA_DIR --enable-shared=no --enable-static=yes && make && make install

#cd $SRC
#curl -O -C - http://diracvideo.org/download/schroedinger/schroedinger-1.0.9.tar.gz
#tar xvzf schroedinger-1.0.9.tar.gz
#cd schroedinger-1.0.9
#./configure --prefix=$TOOLS_MULTIMEDIA_DIR --enable-shared=no --enable-static=yes && make && make install

#FFPLAY (optionnel)
#Cette section est bien sûr optionnelle à moins que l'on veuille bénéficier du lecteur ffplay
#cd $SRC
#curl -O -C - http://www.libsdl.org/release/SDL-1.2.14.tar.gz
#tar xvzf SDL-1.2.14.tar.gz
#cd SDL-1.2.14
#./configure --prefix=$TOOLS_MULTIMEDIA_DIR --enable-static=yes --enable-shared=no && make && make install


echo FFMPEG

cd $SRC
echo "***********************  ffmpeg-$VERSION"
curl -O -C - http://kakola.googlecode.com/files/ffmpeg-$VERSION.tar.gz
curl -O -C - http://kakola.googlecode.com/files/ffmpeg-$VERSION.tar.bz2
[ -d "ffmpeg-$VERSION" ] || tar xvzf ffmpeg-$VERSION.tar.gz
[ -d "ffmpeg-$VERSION" ] || tar xvjf ffmpeg-$VERSION.tar.bz2
#svn checkout svn://svn.ffmpeg.org/ffmpeg/trunk ffmpeg
#git clone git://git.ffmpeg.org/ffmpeg/

#./configure --prefix=$TOOLS_MULTIMEDIA_DIR --enable-libtheora --enable-libvorbis --enable-libmp3lame --enable-libx264 --enable-libxvid --enable-version3 --enable-libfaad --disable-ffplay --disable-ffserver --enable-libfaadbin --enable-libfaac --enable-gpl --enable-nonfree --enable-pthreads --arch=$ARCH

if [ "$VERSION" = "checkout-2011-02-05" ]
then
cd $SRC
cd ffmpeg-checkout-2011-02-05
./configure --prefix=$TOOLS_MULTIMEDIA_DIR  --extra-version="KaKoLa" \
--enable-libtheora --enable-libvorbis --enable-libmp3lame --enable-libx264 --enable-libxvid \
--enable-version3 --disable-ffplay --disable-ffserver --disable-yasm \
--enable-libfaac --enable-gpl --enable-nonfree --enable-pthreads \
--extra-cflags="-I${TOOLS_MULTIMEDIA_DIR}/include/ -I$TOOLS_MULTIMEDIA_DIR/include/a52dec/ -I$TOOLS_MULTIMEDIA_DIR/include/lame/ -I$TOOLS_MULTIMEDIA_DIR/include/ -I$TOOLS_MULTIMEDIA_DIR/include/ao -I$TOOLS_MULTIMEDIA_DIR/include/ogg  -I$TOOLS_MULTIMEDIA_DIR/include/vorbis -I$TOOLS_MULTIMEDIA_DIR/include/theora " \
--extra-ldflags=-L$TOOLS_MULTIMEDIA_DIR/lib --arch=$ARCH  || shellout "./configure ffmpeg-checkout-2011-02-05"
make  || shellout "make ffmpeg-checkout-2011-02-05"
make install || shellout "make install ffmpeg-checkout-2011-02-05"
else
cd ffmpeg-$VERSION
if [ "$VERSION" = "0.6.1" ]
then
	if [ ! -f "ffmpeg-0.6.1_libvpx-v0.9.5-135-gc28b10a.diff" ]
	then
		curl -O -C - http://kakola.googlecode.com/files/ffmpeg-0.6.1_libvpx-v0.9.5-135-gc28b10a.diff
		patch -p1 < ffmpeg-0.6.1_libvpx-v0.9.5-135-gc28b10a.diff  || echo "ERROR patch -p1 < ffmpeg-0.6.1_libvpx-v0.9.5-135-gc28b10a.diff"
	fi
	if [ ! -f "ffmpeg-HEAD-VP8_param_mapping.diff" ]
	then
		curl -O -C - http://kakola.googlecode.com/files/ffmpeg-HEAD-VP8_param_mapping.diff
		patch -p1 < ffmpeg-HEAD-VP8_param_mapping.diff || echo "ERROR patch -p1 < ffmpeg-HEAD-VP8_param_mapping.diff"
	fi
fi
./configure --prefix=$TOOLS_MULTIMEDIA_DIR --extra-version="KaKoLa" --enable-swscale --enable-avfilter --enable-avfilter-lavf \
--enable-libtheora --enable-libvorbis --enable-libmp3lame --enable-libx264 --enable-libxvid \
--enable-version3 --enable-libfaad --disable-ffplay --disable-ffserver --enable-doc \
--enable-libfaadbin --enable-libfaac --enable-gpl --enable-nonfree --enable-pthreads \
--extra-cflags="-I${TOOLS_MULTIMEDIA_DIR}/include/ -I$TOOLS_MULTIMEDIA_DIR/include/a52dec/ -I$TOOLS_MULTIMEDIA_DIR/include/lame/ -I$TOOLS_MULTIMEDIA_DIR/include/ -I$TOOLS_MULTIMEDIA_DIR/include/ao -I$TOOLS_MULTIMEDIA_DIR/include/ogg  -I$TOOLS_MULTIMEDIA_DIR/include/vorbis -I$TOOLS_MULTIMEDIA_DIR/include/theora " \
--extra-ldflags=-L$TOOLS_MULTIMEDIA_DIR/lib --arch=$ARCH  || shellout "./configure ffmpeg-$VERSION"
make  || shellout "make ffmpeg-$VERSION"
make install  || shellout "make install ffmpeg-$VERSION"
fi

TARGET=$TOOLS_MULTIMEDIA_DIR
cd ${SRC}
REV=`svn checkout svn://svn.mplayerhq.hu/mplayer/trunk mplayer | grep "Checked out revision " | sed -e "s/Checked out revision //g" | sed -e "s/\.//g"`
if [ "$REV" = "" ]
then
	echo "Error will getting the revision version of mplayer"
	exit 1
fi

cd mplayer || shellout "cd mplayer"
git clone git://git.ffmpeg.org/ffmpeg.git
if [ -f "mplayer-HEAD-mencoder_VP8_fourcc__2pass_fix.diff" ]
then
	echo "Apply patch mplayer-HEAD-mencoder_VP8_fourcc__2pass_fix.diff"
	curl -O -C - http://kakola.googlecode.com/files/mplayer-HEAD-mencoder_VP8_fourcc__2pass_fix.diff
	patch -p1 < mplayer-HEAD-mencoder_VP8_fourcc__2pass_fix.diff || echo "ERROR patch -p1 < mplayer-HEAD-mencoder_VP8_fourcc__2pass_fix.diff"
fi

./configure --prefix=${TARGET} --extra-cflags="-I${TARGET}/include/ -I${TOOLS_MULTIMEDIA_DIR}/include/ -I$TOOLS_MULTIMEDIA_DIR/include/a52dec/ -I$TOOLS_MULTIMEDIA_DIR/include/lame/ -I$TOOLS_MULTIMEDIA_DIR/include/ -I$TOOLS_MULTIMEDIA_DIR/include/ao -I$TOOLS_MULTIMEDIA_DIR/include/ogg  -I$TOOLS_MULTIMEDIA_DIR/include/vorbis -I$TOOLS_MULTIMEDIA_DIR/include/theora " --extra-ldflags="-L${TARGET}/lib -L$TOOLS_MULTIMEDIA_DIR/lib" --yasm='' || shellout "./configure mplayer"
make -j 4 || shellout "make -j 4 mplayer"
make install || shellout "make install mplayer"

echo ""
echo "All done see ${TARGET} for more details"



exit 0





exit

cd ffmpeg-0.6.1
./configure --enable-gpl \
        --enable-postproc \
        --enable-swscale --enable-avfilter --enable-avfilter-lavf \
        --enable-libmp3lame \
        --enable-libvorbis \
        --enable-libtheora \
        --enable-libdirac --enable-libschroedinger \
        --enable-libfaac \
        --enable-libfaad \
        --enable-libxvid \
        --enable-libx264 \
        --enable-libvpx \
        --enable-libspeex \
	--disable-doc \
        --enable-nonfree \
        --mandir=${prefix}/share/man \
        --enable-shared --enable-pthreads \
        --disable-indevs && make



 
